#include <Lucnx/types.h>

size_t strlen(const char *str)
{
    char *ptr = (char *)str;
    while (*ptr != '\0')
    {
        ptr++;
    }
    return ptr - str;
}

void strcpy(char *dest, char *src)
{
    memcpy(dest, src, strlen(src));
}

void strcast(char *dest, char *src)
{
    int i = strlen(dest);
    strcpy(&dest[i], src);
}

/*
 *  linux/lib/string.c->int strcmp(const char *cs, const char *ct)
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */
int strcmp(const char *cs, const char *ct)
{
    unsigned char c1, c2;

    while (1) {
        c1 = *cs++;
        c2 = *ct++;
        if (c1 != c2)
            return c1 < c2 ? -1 : 1;
        if (!c1)
            break;
    }
    return 0;
}

void memcpy(void *dest, void *src, int count)
{
    char *ptr = dest;
    while (count--)
    {
        *ptr++ = *((char*)src++);
    }
}

void memset(void *dest, u8 data, int count)
{
    char *ptr = dest;
    while (count--)
    {
        *ptr++ = data;
    }
}

int memcmp(const void *_cs, const void *_ct, int count)
{
    char *cs = _cs;
    char *ct = _ct;
    unsigned char c1, c2;

    for (size_t i = 0; i < count; i++)
    {
        c1 = *cs++;
        c2 = *ct++;
        if (c1 != c2)
            return c1 < c2 ? -1 : 1;
    }
    
    return 0;
}